#!/usr/bin/env python
# -*- coding=utf-8 -*-

import datetime

from django.test import TestCase
from django.utils import timezone
from django.core.urlresolvers import reverse

from .models import Question, Choice


# Create your tests here.
class QuestionMethodTest(TestCase):

    def test_is_published_recently_with_future_question(self):

        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(publish_date=time)
        self.assertEqual(future_question.is_published_recently(), False)



    def test_is_published_recently_with_old_question(self):

        time = timezone.now() - datetime.timedelta(days=30)

        old_question = Question(publish_date = time)

        self.assertEqual(old_question.is_published_recently(), False)

    def test_is_published_recently_with_recent_question(self):

        time = timezone.now() - datetime.timedelta(hours=1)
        recent_question = Question(publish_date = time)
        self.assertEqual(recent_question.is_published_recently(), True)


def create_question(content, days):
    """
    method used to create a Question object with content and publish_date with
    days offset to now
    """
    time = timezone.now() + datetime.timedelta(days=days)
    return Question.objects.create(content=content, publish_date=time)

class QuestionViewTest(TestCase):

    def test_index_view_with_no_question(self):
        """
        If no question exist, an appropriate message should be displayed
        """
        response = self.client.get(reverse('poll-index'))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "No polls are available.")
        self.assertQuerysetEqual(response.context['latest_question_list'],[])

    def test_index_view_with_a_past_question(self):

        create_question(content = "Past question.", days = -30)
        response = self.client.get(reverse('poll-index'))
        self.assertQuerysetEqual(response.context['latest_question_list'],['<Question: Past question.>'])

    def test_index_view_with_a_future_question(self):

        create_question(content="Future question.", days=30)
        response = self.client.get(reverse('poll-index'))
        self.assertContains(response,"No polls are available.", status_code=200)
        self.assertQuerysetEqual(response.context['latest_question_list'], [])


    def test_index_view_with_future_and_past_question(self):

        create_question(content="Past question.", days=-30)
        create_question(content="Future question.",days=30)
        response = self.client.get(reverse('poll-index'))

        self.assertQuerysetEqual(
            response.context['latest_question_list'],
            ['<Question: Past question.>']
        )

    def test_index_view_with_two_past_question(self):
        create_question(content="Past question 1.", days=-30)
        create_question(content="Past question 2.", days=-5)

        response = self.client.get(reverse('poll-index'))

        self.assertQuerysetEqual(
            response.context['latest_question_list'],
            ['<Question: Past question 2.>', '<Question: Past question 1.>']
        )


class QuestionIndexDetailViewTest(TestCase):

    def test_detail_view_with_a_future_question(self):
        future_question = create_question(content = "Future question.", days=5)
        response = self.client.get(reverse('poll-detail', args=(future_question.id,)))
        self.assertEqual(response.status_code, 404)

    def test_detail_view_with_a_past_question(self):
        past_question = create_question(content="Past question.", days=-5)

        response = self.client.get(reverse('poll-detail',args=(past_question.id,)))
        self.assertContains(response, past_question.content, status_code=200)





